{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install --upgrade setuptools\n",
    "%pip install --upgrade gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import setuptools\n",
    "\n",
    "import os\n",
    "import sys\n",
    "module_path = os.path.abspath(os.path.join('..'))\n",
    "if module_path not in sys.path:\n",
    "    sys.path.append(module_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import dotenv_values\n",
    "from typechat import Failure, TypeChatValidator, create_language_model\n",
    "from examples.healthData import schema as health\n",
    "from examples.healthData.translator import TranslatorWithHistory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "health_instructions = \"\"\"\n",
    "Help me enter my health data step by step.\n",
    "Ask specific questions to gather required and optional fields I have not already providedStop asking if I don't know the answer\n",
    "Automatically fix my spelling mistakes\n",
    "My health data may be complex: always record and return ALL of it.\n",
    "Always return a response:\n",
    "- If you don't understand what I say, ask a question.\n",
    "- At least respond with an OK message.\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "env_vals = dotenv_values()\n",
    "model = create_language_model(env_vals)\n",
    "validator = TypeChatValidator(health.HealthDataResponse)\n",
    "translator = TranslatorWithHistory(model, validator, health.HealthDataResponse, additional_agent_instructions=health_instructions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas\n",
    "\n",
    "async def get_translation(message, history):\n",
    "    result = await translator.translate(message)\n",
    "    if isinstance(result, Failure):\n",
    "        return f\"Translation Failed ❌ \\n Context: {result.message}\"\n",
    "    else:\n",
    "        result = result.value\n",
    "        output = f\"Translation Succeeded! ✅\\n\"\n",
    "        \n",
    "        data = result.get(\"data\", None)\n",
    "        if data:\n",
    "            df = pandas.DataFrame.from_dict(data)\n",
    "            output += f\"HealthData \\n ``` {df.fillna('').to_markdown(tablefmt='grid')} \\n ```  \\n\"\n",
    "\n",
    "        message = result.get(\"message\", None)\n",
    "        not_translated = result.get(\"notTranslated\", None)\n",
    "\n",
    "        if message:\n",
    "            output += f\"\\n📝: {message}\"\n",
    "            \n",
    "        if not_translated:\n",
    "            output += f\"\\n🤔: I did not understand\\n {not_translated}\" \n",
    "            \n",
    "        return output\n",
    "\n",
    "\n",
    "def get_examples():\n",
    "    example_prompts = []\n",
    "    with open('../examples/healthData/input.txt') as prompts_file:\n",
    "        for line in prompts_file:\n",
    "            example_prompts.append(line)\n",
    "    return example_prompts\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "\n",
    "gr.ChatInterface(get_translation, title=\"💉💊🤧 Health Data\", examples=get_examples()).launch(debug=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
